﻿using System;

using System.IO;
using Microsoft.WindowsAzure.ServiceRuntime;
using NougakudoCompanion.Commons;

namespace NougakudoCompanion.Utils
{
    static public class RuntimeCopy
    {
        const string webFolder = "Utils";

        static public bool Start()
        {
            Logging.Write("Copy start");
            var utils = new string[] { Constants.NOUGAKUGO_CONTRALLER, Constants.NOUGKUDO_LAUNCHER, Constants.EXTRACT_SCRIPT, Constants.AZURE_STORAGE_CLIENT };
            foreach (var i in utils)
            {
                bool azurelib = false;
                if (i == Constants.AZURE_STORAGE_CLIENT) azurelib = true;
                var ret = CopyUtils(i, azurelib);
                if (!ret)
                {
                    Logging.Write("copy faild.");
                    return false;
                }
            }

            return true;
        }

        static bool CopyUtils(string name, bool azurelib = false)
        {
            try
            {
                var workFolder = RoleEnvironment.GetConfigurationSettingValue(Constants.AZURE_CONFIG_WORKING_FOLDER);
                // if vs'item setting is "copy to output directory", the source exsist under role root directory. (Web role have bin directory)
                // if vs'item setting is "not copy", the source exsit under role root directory.
                // The role assembly locate under the role root directory. The role root directory is %RollRoot%\approot or %RoleRoot%\bin.
                var source = System.Environment.ExpandEnvironmentVariables(@"%RoleRoot%\approot\" + Constants.MODULE_LOCATION + @"\" + name);
                //var developmentFabric = bool.Parse(RoleEnvironment.GetConfigurationSettingValue(Constants.AZURE_CONFIG_DEVLOPMENTFABRIC));
                var developmentFabric = RoleEnvironment.IsEmulated;     // Add for Azure SDK 1.5.
                if (developmentFabric)  // development fabric case, then set local copy property to true.
                {
                    source = System.Environment.ExpandEnvironmentVariables(@"%RoleRoot%\approot\bin\" + Constants.MODULE_LOCATION + @"\" + name);
                }
                var webRole = bool.Parse(RoleEnvironment.GetConfigurationSettingValue(Constants.AZURE_CONFIG_USE_WEB_ROLE));
                if (azurelib && webRole)        // web role case.
                    source = System.Environment.ExpandEnvironmentVariables(@"%RoleRoot%\approot\bin\" + name);
                else if (azurelib && !webRole)  // worker role case.
                    source = System.Environment.ExpandEnvironmentVariables(@"%RoleRoot%\approot\" + name);

                var target = Path.Combine(
                             RoleEnvironment.GetLocalResource(Constants.RESOURCE_RUNTIME).RootPath,
                             workFolder, name);
#if DEBUG
                Logging.Write("source = " + source);
                Logging.Write("target = " + target);
#endif
                if (File.Exists(source))
                {
                    Logging.Write("File exsit.");
                }
                else
                {
                    Logging.Write("File not found");
                    return false;
                }

                File.Copy(source, target, true);
                return true;
            }
            catch (Exception e)
            {
                Logging.Write("Copy error = " + e.Message);
                return false;
            }

        }
    }
}